define([index_web_app_name+'js/plugin/topo/node-ui-function.js',
        'topoCustomLink',
        'topoCustomNephogram'],
        function (
        		nodeFunction,CustomLink,CustomNephogram
        		){

Array.prototype.remove=function(dx)
{
	if(isNaN(dx)||dx>this.length){return false;}
	for(var i=0,n=0;i<this.length;i++)
	{
		if(this[i]!=this[dx])
		{
			this[n++]=this[i];
		}
	}
	this.length-=1;
}
	return {
		cloudArray : new Array(),
		putCloud:function(cloud){
			var i = 0;
			for(;i<this.cloudArray.length;i++){
				if(cloud.id == this.cloudArray[i].id ){
					break;
				}
			}
			this.cloudArray[i] = cloud;
		},
		popCloud:function(cloudId){
			var i = 0;
			var temp  = {};
			for(;i<this.cloudArray.length;i++){
				if(cloudId == this.cloudArray[i].id ){
					temp = $.extend({},this.cloudArray[i]);
					this.cloudArray.remove(i);
					break;
				}
			}
			return temp;
		},
		getCloud:function(node){
			for(var i=0;i<this.cloudArray.length;i++){
				for(var j=0;j<this.cloudArray[i]._nodes.length;j++){
					if(this.cloudArray[i]._nodes[j].id == node.id){
						return this.cloudArray[i];
					}
				}
				
			}
			return null;
		},
		toXml:function(scene){

			var nephogramStr = "";
			var cloudNodes = new Array();
			try{
				cloudNodes = scene.findElements(function (a){
	    			return a.type == "cloudNode";
	    		});
				if(!cloudNodes)
					return {nephogramStr:"",cloudNodes:new Array()};
				
				for(var i=0;i<cloudNodes.length;i++){
					nephogramStr += cloudNodes[i].serialize();
				}
				this.clearCloud(scene);
				
			}catch(e){
				console.log(e);
			}
			return {nephogramStr:nephogramStr,cloudNodes:cloudNodes};
		},
		formatCloudByCloudObj:function(scene,cloudObj){
			var nodes = new Array();
			if(cloudObj.node instanceof Array){
				for(var i=0;i<cloudObj.node.length;i++){
					nodes[i] = nodeFunction.findNodeByScene(scene,cloudObj.node[i]._refId);
				}
			}
			else
				nodes[0] = nodeFunction.findNodeByScene(scene,cloudObj.node._refId);

			var saveObj = {};
			saveObj.id = cloudObj._id;
			saveObj.name = cloudObj._name;
			saveObj.desc = cloudObj._desc;
			saveObj.cloudcolor = cloudObj._cloudcolor;
			saveObj.goTopo = cloudObj._goTopo;
			saveObj.x = cloudObj._x;
			saveObj.y = cloudObj._y;
			saveObj.width = cloudObj._width;
			saveObj.height = cloudObj._height;
			
			saveObj.x = cloudObj._x;
			saveObj.y = cloudObj._y;
			saveObj.isShow = true;
			return this.formatCloud(scene,nodes,saveObj);
		},
		findNodeFromSeesion:function (nodes,id){
			for(var i=0;i<nodes.length;i++){
				if(nodes[i].id == id)
					return nodes[i];
			}
			return null;
		},
		formatCloud:function(scene,nodes,saveObj){
			var cloudObj = {};
			cloudObj._id = saveObj.id;
			cloudObj._name = saveObj.name;
			
			//获取矩形
			var box = nodeFunction.getSquareBox(nodes);
			var width = Math.abs(box.endX - box.startX);
			var height = Math.abs(box.endY - box.startY);
			cloudObj._x = (saveObj.x?saveObj.x:(Number(box.startX)+Number(width)/2));
			cloudObj._y = (saveObj.y?saveObj.y:(Number(box.startY)+Number(height)/2));
			cloudObj._desc = saveObj.desc;
			cloudObj._cloudcolor = saveObj.cloudcolor;
			!saveObj.goTopo && (saveObj.goTopo = "");
			cloudObj._goTopo = saveObj.goTopo;
			cloudObj._width = saveObj.width;
			cloudObj._height = saveObj.height;
			
    		var cloudNode = new CustomNephogram(cloudObj);
    		cloudNode.type = "cloudNode";
    		cloudNode.id = cloudObj._id;
			if(		cloudNode.cloudcolor == "white" || 
					cloudNode.cloudcolor == undefined ||
					cloudNode.cloudcolor == "undefined")
				cloudNode.setImage(index_web_app_name+"img/topo/new/cloud/cloud.png");
			else 
				cloudNode.setImage(index_web_app_name+"img/topo/new/cloud/"+cloudNode.cloudcolor+"cloud.png");
    		try{
    			nodeFunction.getNodeEvent().addEventForNode(cloudNode);
    		}catch(e){
    		}
    		pObj = this;
//    		if(nodeFunction.getNodeEntry().isEditer == true)
    		{
    			
        		cloudNode.dbclick(function(){
    				//展开云,保存云节点
        			pObj.putCloud(this);
        			//删除云节点
        			pObj.removeCloud(scene,this);
        		});
    		}
    		
    		
    		scene.add(cloudNode);
    		//先连线，检查已节点为起点的终点或起点
    		//再删除节点
    		var lines = scene.findElements(function (a){
    			return a.type == "dependence";
    		});
    		//临时存储
    		cloudNode._nodes = new Array();
    		cloudNode._lines = new Array();
    		function checkInCloud(startId,endId,nodes){
    			var length = 0;
    			for(var i=0;i<nodes.length;i++){
    				if(startId == nodes[i].id ){
    					length = length +1;
    				}
    				else if(endId == nodes[i].id ){
    					length = length +1;
    				}
    				if(length >= 2)
    					return true;
    			}
    			return false;
    		}
    		function chechHaveLink(id,links){
    			for(var i=0;i<links.length;i++){
    				if(links[i].id == id)
    					return true;
    			}
    			return false;
    		}
    		function putInArray(arry,obj){
    			var i=0;
    			for(;i<arry.length;i++)
    			{
    				if(arry[i].id == obj.id){
    					return;
    				}
    			}
    			arry[i] = obj;
    		}
    		for(var i=0;i<nodes.length;i++){
    			var index = 0;
//    			var _lineObj = null;
    			for(var j=0;j<lines.length;j++){
    				var ids = lines[j].id.split("::");
    				var lineObj = $.extend({},lines[j].lineObj);
    				!ids[2] && (ids[2] = index);
    				index = index+1;
    				

    				if(checkInCloud(ids[0],ids[1],nodes) == true){
    					if(chechHaveLink(lines[j].id,cloudNode._lines) == false){
        					cloudNode._lines[cloudNode._lines.length] = lines[j];
        					scene.remove(lines[j]);
    					}
    					continue;
    				}

    				if(ids[0] == nodes[i].id || ids[1] == nodes[i].id)
    				{

        				//为起点
        				if(ids[0] == nodes[i].id){
        					lineObj.id = cloudNode.id+"::"+ids[1]+"::"+ids[2];
        					lineObj._startId = cloudNode.id;
        					lineObj._endId = ids[1];
        				}
        				//为终点
        				else if(ids[1] == nodes[i].id){
        					lineObj.id = ids[0]+"::"+cloudNode.id+"::"+ids[2];
        					lineObj._startId = ids[0];
        					lineObj._endId = cloudNode.id;
        				}
//        				if(_lineObj==null)
//        					_lineObj = lineObj;
    					//this.createLink(scene,lineObj);
    					//缓存连线
        				var createLinkState = true;
    					for(var l=0;l<cloudNode._lines.length;l++)
    					{
    						try{
    							
        						if(cloudNode._lines[l].newId.indexOf(ids[0]+"::"+cloudNode.id)!=-1 || 
        								cloudNode._lines[l].newId.indexOf(cloudNode.id+"::"+ids[1])!=-1){
        							createLinkState = false;
        							break;
        						}
    						}catch(e){
    							
    						}

    					}
    					if(createLinkState == true)
    						this.createLink(scene,lineObj);
    					lines[j].newId = lineObj.id;
    					cloudNode._lines[cloudNode._lines.length] = lines[j];


    					
    					//删除连线
    					scene.remove(lines[j]);
    				}

    			}
//    			if(_lineObj!=null)
//    			this.createLink(scene,_lineObj);
    		}
			if(!cloudNode._redLine)
				cloudNode._redLine = new Array();
			if(!cloudNode._yelLine)
				cloudNode._yelLine = new Array();
    		for(var i=0;i<cloudNode._lines.length;i++)
    		{
				if(saveObj.isShow)
				{
					if(		cloudNode._lines[i].lineObj._color == "red" )
					{
						putInArray(cloudNode._redLine,cloudNode._lines[i]);
					}
					else if(cloudNode._lines[i].lineObj._color == "yellow"){
						putInArray(cloudNode._yelLine,cloudNode._lines[i]);
					}
				}
    		}
    		for(var i=0;i<nodes.length;i++){
    			//缓存节点
    			cloudNode._nodes[cloudNode._nodes.length] = nodes[i];
    			scene.remove(nodes[i]);
    		}
    		if(saveObj.isShow && (cloudNode._redLine || cloudNode._yelLine))
    		{
    			cloudNode.isShow = true;
    			if(cloudNode._redLine.length > 0)
    			{
    				cloudNode.setImage(index_web_app_name + "img/topo/new/cloud/redcloud.png");
    			}
    			else if(cloudNode._yelLine.length > 0){
    				cloudNode.setImage(index_web_app_name + "img/topo/new/cloud/yellowcloud.png");
    			}
    			cloudNode.addEventListener('mouseover',function (event){
    				var div = $('<div id="tip_div" class="pabs" style="top:'+(event.pageY+20)+'px;left:'+(event.pageX+20)+'px;background-color:#FFF8DC;padding:5px;border-radius:5px;"></div>');
    				if(this._redLine)
    				{
    					for(var i=0;i<this._redLine.length;i++)
    					{
            				var form_group = $('<div class="form-group" style="margin:0;"></div>');
            				var label_name = $('<label >'+this._redLine[i].desc+'</label>');
            				form_group.append(label_name);
            				div.append(form_group);
    					}
    				}
    				if(this._yelLine)
    				{
    					for(var i=0;i<this._yelLine.length;i++)
    					{
            				var form_group = $('<div class="form-group" style="margin:0;"></div>');
            				var label_name = $('<label >'+this._yelLine[i].desc+'</label>');
            				form_group.append(label_name);
            				div.append(form_group);
    					}
    				}
    				
    				$('#content_div').append(div);
    			});
    			cloudNode.addEventListener('mouseout',function (event){
    				$('#content_div').find('#tip_div').remove();
    			});
    		}
    		return cloudNode;
		},
		loadNetPorts:function(){
			
		},
		showLineData:function(link){
			
		},
		createLink:function(scene,lineObj){
			var link = null;
			try{
				link = new CustomLink(lineObj,scene);
			}catch(e){
				return;
			}
			 link.id = lineObj.id;
			 link.type = "dependence";
			 scene.add(link);
			 try{
				 nodeFunction.getNodeEvent().linkDbClick(link);
			 }catch(e){
			 }
	    		//固定颜色,如果没有修改
			 if(nodeFunction.getNodeEntry().isEditer == true)
				 link.strokeColor = "0,200,255";
			 this.showLineData(link);
			 //this.loadNetPorts({node:link});
		},
		//清除全部云节点数据
		clearCloud:function(scene){
			//删除连接云节点的连线
			var lines = scene.findElements(function (a){
				var id = ""+a.id;
    			return id.indexOf("cloud") != -1 && a.type == "dependence";
    		});
			for(var i=0;i<lines.length;i++){
				scene.remove(lines[i]);
			}
			//先反向全删除
			var cloudNodes = scene.findElements(function (a){
    			return a.type == "cloudNode";
    		});
			for(var i=0;i<cloudNodes.length;i++){
				this.backUpNode(scene, cloudNodes[i]);
			}
			for(var i=0;i<cloudNodes.length;i++){
				this.backUpLink(scene, cloudNodes[i]);
			}
			for(var i=0;i<cloudNodes.length;i++){
				scene.remove(cloudNodes[i]);
			}
		},
		//删除云节点
		removeCloud:function(scene,cloudNode,model){
			//删除连接云节点的连线
			var lines = scene.findElements(function (a){
				var id = ""+a.id;
    			return id.indexOf(cloudNode.id+"::") != -1 || id.indexOf("::"+cloudNode.id) != -1;
    		});
			for(var i=0;i<lines.length;i++){
				scene.remove(lines[i]);
			}
			//先反向全删除
			var cloudNodes = scene.findElements(function (a){
    			return a.type == "cloudNode";
    		});
			this.backUpNode(scene, cloudNode);
			for(var i=0;i<cloudNodes.length;i++){
				if(cloudNode.id != cloudNodes[i].id)
				this.backUpNode(scene, cloudNodes[i]);
			}
			for(var i=0;i<cloudNodes.length;i++){
				if(cloudNode.id != cloudNodes[i].id)
				this.backUpLink(scene, cloudNodes[i]);
			}
			this.backUpLink(scene, cloudNode);
			
			//删除云节点
			scene.remove(cloudNode);
			for(var i=0;i<cloudNodes.length;i++){
				scene.remove(cloudNodes[i]);
			}
			//创建云节点
			for(var i=0;i<cloudNodes.length;i++){
				if(cloudNode.id != cloudNodes[i].id){
					this.formatCloud(scene,cloudNodes[i]._nodes,cloudNodes[i]);
				}
					
			}
			//this.backUpNode(scene, cloudNode);
			//this.backUpLink(scene, cloudNode);

//			scene.remove(cloudNode);
		},
		//还原节点
		backUpNode:function(scene,cloudNode){
			for(var i=0;i<cloudNode._nodes.length;i++){
	    		scene.add(cloudNode._nodes[i]);
			}
		},
		//还原连线
		backUpLink:function(scene,cloudNode){
			for(var i=0;i<cloudNode._lines.length;i++){
				this.createLink(scene, cloudNode._lines[i].lineObj);
//	    		scene.add(cloudNode._lines[i]);
			}
		},
	};
	
});
